import { login } from "@/services";
import { setToken } from "@/utils";
import { ILoginForm, IUserInfo } from "@/utils/types";
import { makeAutoObservable, runInAction } from "mobx"

class User{
  constructor(rootState: any) {
    this.rootState = rootState;
    makeAutoObservable(this)
  }
  isLogin = false;
  rootState = null;
  userInfo: IUserInfo = {} as IUserInfo;

  async login(form: ILoginForm){
    let result = await login(form);
    if (result.data){
      runInAction(()=>{
        this.userInfo = result.data;
        this.isLogin = true;
      })
    }
    // 存储登陆态
    setToken(result.data.token)
    return true;
  }

  async setLogin(){
    setInterval(() =>{
      runInAction(()=> {
        this.isLogin = !this.isLogin
      })
    }, 1000);
  }
}

export default User;